package yao.spider.jsoup.xiangrikui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;

import yao.common.excel.ExcelCreator;
import yao.common.excel.ExcelData;
import yao.common.file.FileUtils;

public abstract class XiangRiKuiSpider {
	private static final String keys[] = { "name", "company", "mobile", "mobileId"};// map中的key
	private static final String columnNames[] = { "姓名", "所属公司", "电话号码","mobileId" };// 列名
	
	private static final String MOBILE_URL_PREFIX = "http://common.xiangrikui.com/api/users/";
		
	private final String errorUrlPath;
	private final ExcelCreator excelCreator;
	
	protected XiangRiKuiSpider(String errorFilePath, ExcelCreator excelCreator) {
		super();
		this.errorUrlPath = errorFilePath;
		this.excelCreator = excelCreator;
	}
	
	public static XiangRiKuiSpider createOfReplenish(String errorFilePath,ExcelCreator excelCreator, String path, String fileMatchExpression) {
		return new XiangRiKuiReplenishSpider(errorFilePath, excelCreator, path, fileMatchExpression);
	}
	
	public static XiangRiKuiSpider createOfRange(String errorFilePath, ExcelCreator excelCreator, int firstPage, int endPage) {
		return new XiangRiKuiRangeSpider(errorFilePath, excelCreator, firstPage, endPage);
	}

	public void exportExcel() throws IOException {
		List<Map<String,Object>> agents = this.getAgents();		
		ExcelData ed = new ExcelData();
		ed.setKeys(keys);
		ed.setColumnNames(columnNames);
		ed.setContent(agents);			
		this.excelCreator.download(ed);		
	}

	private List<Map<String, Object>> getAgents() {
		List<Map<String, Object>> mapList = new ArrayList<>();
		List<String> urlsOfAgent = this.getUrls();
		for(String url : urlsOfAgent) {
			System.out.println("agent url = " + url);
			Document document = null;
			try {
				document = Jsoup.connect(url).get();
			} catch (IOException e1) {
				e1.printStackTrace();
				FileUtils.append(this.errorUrlPath, url);
				continue;
			}
			//所有代理人
			Element element = document.getElementById("agents");
			Elements agents = element.getElementsByClass("agent-info");
			for(Element agent : agents) {
				Map<String, Object> map = new HashMap<String, Object>();
				String name = agent.getElementsByClass("agent-name").get(0).text();
				map.put("name", name);
				
				String company = agent.getElementsByClass("agent-company").get(0).html();
				map.put("company", company);
				
				String mobileId = agent.getElementsByClass("callButton").get(0).attr("data-call-id");
				map.put("mobileId", mobileId);				
				
				String mobile = this.getMobile(mobileId);
				map.put("mobile", mobile);				
				mapList.add(map);
			}
		}
		return mapList;
	}
	
	private String getMobile(String id) {
		if(StringUtils.isBlank(id)) {
			return null;
		}
		String url = MOBILE_URL_PREFIX + id;
		MobileRespMsg mobileMsg = null;
		try {
			Document document = Jsoup.connect(url).header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0")
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8").ignoreContentType(true).get();
			String data = document.body().html();
			mobileMsg = JSON.parseObject(data, MobileRespMsg.class);
			if(!mobileMsg.getStatus_code().equals("200")) {
				System.out.println("id=" + id + ", get mobile error,code=" + mobileMsg.getStatus_code());
				return null;
			}
			return mobileMsg.getPhone();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected abstract List<String> getUrls();
	
	
}
